package com.wjh.kt_live.service.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wjh.course.CourseFeignClient;
import com.wjh.kt_live.mapper.LiveCourseMapper;
import com.wjh.kt_live.service.LiveCourseAccountService;
import com.wjh.kt_live.service.LiveCourseDescriptionService;
import com.wjh.kt_live.service.LiveCourseService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wjh.kt_live.utils.CommonResult;
import com.wjh.kt_live.utils.MTCloud;
import com.wjh.kt_vod.exception.GgkException;
import com.wjh.kt_vod.model.live.LiveCourse;
import com.wjh.kt_vod.model.live.LiveCourseAccount;
import com.wjh.kt_vod.model.live.LiveCourseDescription;
import com.wjh.kt_vod.model.vod.Teacher;
import com.wjh.kt_vod.vo.live.LiveCourseFormVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.HashMap;
import java.util.List;

/**
 * <p>
 * 直播课程表 服务实现类
 * </p>
 *
 * @author Wang_Junhong
 * @since 2022-07-20 05:48:33
 */
@Service
public class LiveCourseServiceImpl extends ServiceImpl<LiveCourseMapper, LiveCourse> implements LiveCourseService {

    @Autowired
    private CourseFeignClient teacherFeignClient;
    @Autowired
    private LiveCourseDescriptionService liveCourseDescriptionService;
    @Autowired
    private LiveCourseAccountService liveCourseAccountService;
    @Autowired
    private MTCloud mtCloudClient;

    @Override
    public IPage<LiveCourse> selectPage(Page<LiveCourse> liveCoursePage) {
        Page<LiveCourse> liveCoursePageModel= baseMapper.selectPage(liveCoursePage, null);

        List<LiveCourse> records = liveCoursePageModel.getRecords();
        records.stream().forEach(item ->{

            Long teacherId = item.getTeacherId();
            Teacher teacherInfo = teacherFeignClient.getTeacherInfo(teacherId);
            if (!StringUtils.isEmpty(teacherInfo)){
                item.getParam().put("teacherName",teacherInfo.getName());
                item.getParam().put("teacherLevel",teacherInfo.getLevel());
            }

        });
        return liveCoursePageModel;
    }

    /**
     * 直播添加
     * @param liveCourseFormVo
     */
    @Override
    public void saveLive(LiveCourseFormVo liveCourseFormVo) {

        LiveCourse liveCourse = new LiveCourse();
        BeanUtils.copyProperties(liveCourseFormVo,liveCourse);

        Teacher teacher = teacherFeignClient.getTeacherInfo(liveCourseFormVo.getTeacherId());

        HashMap<Object, Object> objectObjectHashMap = new HashMap<>();
        objectObjectHashMap.put("scenes",1);
        objectObjectHashMap.put("password",liveCourseFormVo.getPassword());

        try {
            String s = mtCloudClient.courseAdd(liveCourse.getCourseName(), teacher.getId().toString()
                    , liveCourse.getStartTime().toString(), liveCourse.getEndTime().toString(), teacher.getName()
                    , teacher.getIntro(), objectObjectHashMap);
            System.out.println("res:"+s);

            CommonResult<JSONObject> commonResult = JSON.parseObject(s, CommonResult.class);
            //表示创建成功
            if (Integer.parseInt(commonResult.getCode())==MTCloud.CODE_SUCCESS){
//                添加直播基本信息
                JSONObject data = commonResult.getData();
                Long course_id = data.getLong("course_id");
                liveCourse.setCourseId(course_id);
                baseMapper.insert(liveCourse);
//                添加直播描述信息
                LiveCourseDescription liveCourseDescription = new LiveCourseDescription();
                liveCourseDescription.setLiveCourseId(liveCourseFormVo.getId());
                liveCourseDescription.setDescription(liveCourseFormVo.getDescription());
                liveCourseDescriptionService.save(liveCourseDescription);
//                添加直播账号信息
                LiveCourseAccount liveCourseAccount = new LiveCourseAccount();
                liveCourseAccount.setLiveCourseId(liveCourse.getId());
                liveCourseAccount.setZhuboAccount(data.getString("bid"));
                liveCourseAccount.setZhuboPassword(liveCourseFormVo.getPassword());
                liveCourseAccount.setAdminKey(data.getString("admin_key"));
                liveCourseAccount.setUserKey("user_key");
                liveCourseAccount.setZhuboKey("zhubo_key");
                liveCourseAccountService.save(liveCourseAccount);
            }else {//失败
                throw new GgkException(20001,commonResult.getmsg());
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    /**
     * 删除直播
     * @param id
     */
    @Override
    public void removeLive(Long id) {
//        根据id查询直播课程信息
        LiveCourse liveCourse = baseMapper.selectById(id);
        if (!StringUtils.isEmpty(liveCourse)){
            Long courseId = liveCourse.getCourseId();
            try {
                mtCloudClient.courseDelete(courseId.toString());
                baseMapper.deleteById(id);
                liveCourseDescriptionService.removeById(id);
                liveCourseAccountService.removeById(id);
            } catch (Exception e) {
                throw new GgkException(20001,"删除直播课程失败");
            }
        }

    }

}
